//
//  HJGroupChatTopView.m
//  KaiChat
//
//  Created by lx on 2023/11/16.
//

#import "HJGroupChatTopView.h"
#import "HJPageControl.h"

@interface HJGroupChatTopCell: UICollectionViewCell

@property (nonatomic, weak) UIImageView *iconView;
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UIView *pointView;
@property (nonatomic, strong) HJCellModel *model;

@end


@implementation HJGroupChatTopCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        UIImageView *iconView = [UIImageView addToView:self.contentView];
        iconView.image = [UIImage imageNamed:@"createGroup"];
        [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.offset(0);
            make.width.height.offset(20);
            make.left.offset(15);
        }];
        
        UILabel *titleLabel = [UILabel lableWithSuperView:self.contentView font:UIFontMake(14) color:UIColor.tj_333333Color title:nil textAlignment:NSTextAlignmentLeft];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(iconView.mas_right).offset(10);
            make.centerY.offset(0);
            make.right.inset(50);
        }];

        UIImageView *arrowView = [UIImageView addToView:self.contentView];
        arrowView.image = [UIImage imageNamed:@"cell_arrow"];
        [arrowView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.offset(0);
            make.right.inset(15);
        }];
        
        UIView *pointView = [UIView addToView:self.contentView];
        pointView.addCornerRadius(5);
        pointView.hidden = YES;
        pointView.backgroundColor = UIColor.tj_FF5100Color;
        [pointView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(arrowView.mas_left).inset(8);
            make.centerY.offset(0);
            make.width.height.offset(10);
        }];
        
        _pointView = pointView;
        _iconView = iconView;
        _titleLabel = titleLabel;
    }
    return self;
}

- (void)setModel:(HJCellModel *)model {
    _model = model;
    _iconView.image = [UIImage imageNamed:model.iconName];
    _titleLabel.text = model.text;
    _pointView.hidden = ![model.identifier isEqualToString:@"request"];
}

@end


@interface HJGroupChatTopView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, weak) HJPageControl *pageControl;

@end


@implementation HJGroupChatTopView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.collectionView.addCornerRadius(5);
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.inset(leftRightMargin);
            make.height.offset(40);
            make.bottom.offset(0);
        }];
        
        HJPageControl *pageControl = [[HJPageControl alloc] init];
        pageControl.currentPage = 0;
        pageControl.currentPageIndicatorTintColor = UIColor.tj_mainColor;
        pageControl.pageIndicatorTintColor = UIColor.tj_F6F6F6Color;
        [self addSubview:pageControl];
        [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.offset(0);
            make.width.offset(50);
            make.height.offset(12);
            make.bottom.equalTo(self.collectionView.mas_bottom);
        }];
        
        _pageControl = pageControl;
    }
    return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HJGroupChatTopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HJGroupChatTopCell" forIndexPath:indexPath];
    cell.model = self.datas[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (_didClickItemBlock) {
        _didClickItemBlock(self.datas[indexPath.row]);
    }
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _flowLayout = flowLayout;
        flowLayout.itemSize = CGSizeMake(KScreenWidth - leftRightMargin * 2, 40);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.pagingEnabled = YES;
        _collectionView.bounces = NO;
        [_collectionView registerClass:HJGroupChatTopCell.class forCellWithReuseIdentifier:@"HJGroupChatTopCell"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.alwaysBounceHorizontal=YES;
        
    }
    return _collectionView;
}

- (void)setDatas:(NSArray *)datas {
    _datas = datas;
   
    self.pageControl.numberOfPages = datas.count;
    self.pageControl.hidden = datas.count <= 1;
    [self.collectionView reloadData];
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.pageControl.currentPage = scrollView.contentOffset.x / self.collectionView.frame.size.width;
    
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    self.pageControl.currentPage = scrollView.contentOffset.x / self.collectionView.frame.size.width;
}

- (void)reloadData {
    [self.collectionView reloadData];
}

@end
